# -*- Mode: makefile -*-

.PHONY: check-help
check-help:
	@echo "Regression testing targets:"
	@echo
	@echo " $(MAKE) check                Run block, qapi-schema, unit, softfloat, qtest and decodetree tests"
	@echo
	@echo " $(MAKE) check-qtest-TARGET   Run qtest tests for given target"
	@echo " $(MAKE) check-qtest          Run qtest tests"
	@echo " $(MAKE) check-unit           Run qobject tests"
	@echo " $(MAKE) check-speed          Run qobject speed tests"
	@echo " $(MAKE) check-qapi-schema    Run QAPI schema tests"
	@echo " $(MAKE) check-block          Run block tests"
ifeq ($(CONFIG_TCG),y)
	@echo " $(MAKE) check-tcg            Run TCG tests"
	@echo " $(MAKE) check-softfloat      Run FPU emulation tests"
endif
	@echo " $(MAKE) check-acceptance     Run all acceptance (functional) tests"
	@echo
	@echo " $(MAKE) check-report.tap     Generates an aggregated TAP test report"
	@echo " $(MAKE) check-venv           Creates a Python venv for tests"
	@echo " $(MAKE) check-clean          Clean the tests and related data"
	@echo
	@echo "The following are useful for CI builds"
	@echo " $(MAKE) check-build          Build most test binaris"
	@echo " $(MAKE) get-vm-images        Downloads all images used by acceptance tests, according to configured targets (~350 MB each, 1.5 GB max)"
	@echo
	@echo
	@echo "The variable SPEED can be set to control the gtester speed setting."
	@echo "Default options are -k and (for $(MAKE) V=1) --verbose; they can be"
	@echo "changed with variable GTESTER_OPTIONS."

ifneq ($(wildcard config-host.mak),)
export SRC_PATH

# TODO don't duplicate $(SRC_PATH)/Makefile's qapi-py here
qapi-py = $(SRC_PATH)/scripts/qapi/__init__.py \
$(SRC_PATH)/scripts/qapi/commands.py \
$(SRC_PATH)/scripts/qapi/common.py \
$(SRC_PATH)/scripts/qapi/doc.py \
$(SRC_PATH)/scripts/qapi/error.py \
$(SRC_PATH)/scripts/qapi/events.py \
$(SRC_PATH)/scripts/qapi/expr.py \
$(SRC_PATH)/scripts/qapi/gen.py \
$(SRC_PATH)/scripts/qapi/introspect.py \
$(SRC_PATH)/scripts/qapi/parser.py \
$(SRC_PATH)/scripts/qapi/schema.py \
$(SRC_PATH)/scripts/qapi/source.py \
$(SRC_PATH)/scripts/qapi/types.py \
$(SRC_PATH)/scripts/qapi/visit.py \
$(SRC_PATH)/scripts/qapi-gen.py

# Get the list of all supported sysemu targets
SYSEMU_TARGET_LIST := $(subst -softmmu.mak,,$(notdir \
   $(wildcard $(SRC_PATH)/default-configs/*-softmmu.mak)))

check-unit-y += tests/check-qdict$(EXESUF)
check-unit-y += tests/check-block-qdict$(EXESUF)
check-unit-y += tests/check-qnum$(EXESUF)
check-unit-y += tests/check-qstring$(EXESUF)
check-unit-y += tests/check-qlist$(EXESUF)
check-unit-y += tests/check-qnull$(EXESUF)
check-unit-y += tests/check-qobject$(EXESUF)
check-unit-y += tests/check-qjson$(EXESUF)
check-unit-y += tests/check-qlit$(EXESUF)
check-unit-y += tests/test-qobject-output-visitor$(EXESUF)
check-unit-y += tests/test-clone-visitor$(EXESUF)
check-unit-y += tests/test-qobject-input-visitor$(EXESUF)
check-unit-$(CONFIG_SOFTMMU) += tests/test-qmp-cmds$(EXESUF)
check-unit-y += tests/test-string-input-visitor$(EXESUF)
check-unit-y += tests/test-string-output-visitor$(EXESUF)
check-unit-y += tests/test-qmp-event$(EXESUF)
check-unit-y += tests/test-opts-visitor$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-coroutine$(EXESUF)
check-unit-y += tests/test-visitor-serialization$(EXESUF)
check-unit-$(CONFIG_SOFTMMU) += tests/test-iov$(EXESUF)
check-unit-y += tests/test-bitmap$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-aio$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-aio-multithread$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-throttle$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-thread-pool$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-hbitmap$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-bdrv-drain$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-bdrv-graph-mod$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-blockjob$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-blockjob-txn$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-block-backend$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-block-iothread$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-image-locking$(EXESUF)
check-unit-y += tests/test-x86-cpuid$(EXESUF)
# all code tested by test-x86-cpuid is inside topology.h
ifeq ($(CONFIG_SOFTMMU),y)
check-unit-y += tests/test-xbzrle$(EXESUF)
check-unit-$(CONFIG_POSIX) += tests/test-vmstate$(EXESUF)
endif
check-unit-y += tests/test-cutils$(EXESUF)
check-unit-y += tests/test-shift128$(EXESUF)
check-unit-y += tests/test-mul64$(EXESUF)
check-unit-y += tests/test-int128$(EXESUF)
# all code tested by test-int128 is inside int128.h
check-unit-y += tests/rcutorture$(EXESUF)
check-unit-y += tests/test-rcu-list$(EXESUF)
check-unit-y += tests/test-rcu-simpleq$(EXESUF)
check-unit-y += tests/test-rcu-tailq$(EXESUF)
check-unit-y += tests/test-rcu-slist$(EXESUF)
check-unit-y += tests/test-qdist$(EXESUF)
check-unit-y += tests/test-qht$(EXESUF)
check-unit-y += tests/test-qht-par$(EXESUF)
check-unit-y += tests/test-bitops$(EXESUF)
check-unit-y += tests/test-bitcnt$(EXESUF)
check-unit-y += tests/check-qom-interface$(EXESUF)
check-unit-y += tests/check-qom-proplist$(EXESUF)
check-unit-y += tests/test-qemu-opts$(EXESUF)
check-unit-y += tests/test-keyval$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-write-threshold$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-crypto-hash$(EXESUF)
check-speed-$(CONFIG_BLOCK) += tests/benchmark-crypto-hash$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-crypto-hmac$(EXESUF)
check-speed-$(CONFIG_BLOCK) += tests/benchmark-crypto-hmac$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-crypto-cipher$(EXESUF)
check-speed-$(CONFIG_BLOCK) += tests/benchmark-crypto-cipher$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-crypto-secret$(EXESUF)
check-unit-$(call land,$(CONFIG_BLOCK),$(CONFIG_GNUTLS)) += tests/test-crypto-tlscredsx509$(EXESUF)
check-unit-$(call land,$(CONFIG_BLOCK),$(CONFIG_GNUTLS)) += tests/test-crypto-tlssession$(EXESUF)
ifndef CONFIG_TSAN
# Some tests: test-char, test-qdev-global-props, and test-qga,
# are not runnable under TSan due to a known issue.
# https://github.com/google/sanitizers/issues/1116
check-unit-$(CONFIG_SOFTMMU) += tests/test-char$(EXESUF)
check-unit-$(CONFIG_SOFTMMU) += tests/test-qdev-global-props$(EXESUF)
ifneq (,$(findstring qemu-ga,$(TOOLS)))
check-unit-$(call land,$(CONFIG_LINUX),$(CONFIG_VIRTIO_SERIAL)) += tests/test-qga$(EXESUF)
endif
endif
check-unit-$(CONFIG_SOFTMMU) += tests/test-timed-average$(EXESUF)
check-unit-$(call land,$(CONFIG_SOFTMMU),$(CONFIG_INOTIFY1)) += tests/test-util-filemonitor$(EXESUF)
check-unit-$(CONFIG_SOFTMMU) += tests/test-util-sockets$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-authz-simple$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-authz-list$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-authz-listfile$(EXESUF)
check-unit-$(call land,$(CONFIG_BLOCK),$(CONFIG_AUTH_PAM)) += tests/test-authz-pam$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-io-task$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-io-channel-socket$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-io-channel-file$(EXESUF)
check-unit-$(call land,$(CONFIG_BLOCK),$(CONFIG_GNUTLS)) += tests/test-io-channel-tls$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-io-channel-command$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-io-channel-buffer$(EXESUF)
check-unit-$(CONFIG_SOFTMMU) += tests/test-base64$(EXESUF)
check-unit-$(call land,$(CONFIG_BLOCK),$(if $(CONFIG_NETTLE),y,$(CONFIG_GCRYPT))) += tests/test-crypto-pbkdf$(EXESUF)
check-unit-$(CONFIG_BLOCK) += tests/test-crypto-ivgen$(EXESUF)
check-unit-$(CONFIG_BLOCK)  += tests/test-crypto-afsplit$(EXESUF)
check-unit-$(call land,$(CONFIG_BLOCK),$(CONFIG_QEMU_PRIVATE_XTS)) += tests/test-crypto-xts$(EXESUF)
check-unit-$(CONFIG_BLOCK)  += tests/test-crypto-block$(EXESUF)
check-unit-y += tests/test-logging$(EXESUF)
check-unit-$(call land,$(CONFIG_BLOCK),$(CONFIG_REPLICATION)) += tests/test-replication$(EXESUF)
check-unit-$(CONFIG_SOFTMMU) += tests/test-bufferiszero$(EXESUF)
check-unit-y += tests/test-uuid$(EXESUF)
check-unit-y += tests/ptimer-test$(EXESUF)
check-unit-y += tests/test-qapi-util$(EXESUF)

check-block-$(call land,$(CONFIG_POSIX),$(CONFIG_SOFTMMU)) += tests/check-block.sh

qapi-schema += alternate-any.json
qapi-schema += alternate-array.json
qapi-schema += alternate-base.json
qapi-schema += alternate-branch-if-invalid.json
qapi-schema += alternate-clash.json
qapi-schema += alternate-conflict-dict.json
qapi-schema += alternate-conflict-enum-bool.json
qapi-schema += alternate-conflict-enum-int.json
qapi-schema += alternate-conflict-string.json
qapi-schema += alternate-conflict-bool-string.json
qapi-schema += alternate-conflict-num-string.json
qapi-schema += alternate-empty.json
qapi-schema += alternate-invalid-dict.json
qapi-schema += alternate-nested.json
qapi-schema += alternate-unknown.json
qapi-schema += args-alternate.json
qapi-schema += args-any.json
qapi-schema += args-array-empty.json
qapi-schema += args-array-unknown.json
qapi-schema += args-bad-boxed.json
qapi-schema += args-boxed-anon.json
qapi-schema += args-boxed-string.json
qapi-schema += args-int.json
qapi-schema += args-invalid.json
qapi-schema += args-member-array-bad.json
qapi-schema += args-member-case.json
qapi-schema += args-member-unknown.json
qapi-schema += args-name-clash.json
qapi-schema += args-union.json
qapi-schema += args-unknown.json
qapi-schema += bad-base.json
qapi-schema += bad-data.json
qapi-schema += bad-ident.json
qapi-schema += bad-if.json
qapi-schema += bad-if-empty.json
qapi-schema += bad-if-empty-list.json
qapi-schema += bad-if-list.json
qapi-schema += bad-type-bool.json
qapi-schema += bad-type-dict.json
qapi-schema += bad-type-int.json
qapi-schema += base-cycle-direct.json
qapi-schema += base-cycle-indirect.json
qapi-schema += command-int.json
qapi-schema += comments.json
qapi-schema += doc-bad-alternate-member.json
qapi-schema += doc-bad-boxed-command-arg.json
qapi-schema += doc-bad-command-arg.json
qapi-schema += doc-bad-enum-member.json
qapi-schema += doc-bad-event-arg.json
qapi-schema += doc-bad-feature.json
qapi-schema += doc-bad-section.json
qapi-schema += doc-bad-symbol.json
qapi-schema += doc-bad-union-member.json
qapi-schema += doc-before-include.json
qapi-schema += doc-before-pragma.json
qapi-schema += doc-duplicated-arg.json
qapi-schema += doc-duplicated-return.json
qapi-schema += doc-duplicated-since.json
qapi-schema += doc-empty-arg.json
qapi-schema += doc-empty-section.json
qapi-schema += doc-empty-symbol.json
qapi-schema += doc-good.json
qapi-schema += doc-interleaved-section.json
qapi-schema += doc-invalid-end.json
qapi-schema += doc-invalid-end2.json
qapi-schema += doc-invalid-return.json
qapi-schema += doc-invalid-section.json
qapi-schema += doc-invalid-start.json
qapi-schema += doc-missing-colon.json
qapi-schema += doc-missing-expr.json
qapi-schema += doc-missing-space.json
qapi-schema += doc-missing.json
qapi-schema += doc-no-symbol.json
qapi-schema += doc-undoc-feature.json
qapi-schema += double-type.json
qapi-schema += duplicate-key.json
qapi-schema += empty.json
qapi-schema += enum-bad-member.json
qapi-schema += enum-bad-name.json
qapi-schema += enum-bad-prefix.json
qapi-schema += enum-clash-member.json
qapi-schema += enum-dict-member-unknown.json
qapi-schema += enum-if-invalid.json
qapi-schema += enum-int-member.json
qapi-schema += enum-member-case.json
qapi-schema += enum-missing-data.json
qapi-schema += enum-wrong-data.json
qapi-schema += event-boxed-empty.json
qapi-schema += event-case.json
qapi-schema += event-member-invalid-dict.json
qapi-schema += event-nest-struct.json
qapi-schema += features-bad-type.json
qapi-schema += features-deprecated-type.json
qapi-schema += features-duplicate-name.json
qapi-schema += features-if-invalid.json
qapi-schema += features-missing-name.json
qapi-schema += features-name-bad-type.json
qapi-schema += features-no-list.json
qapi-schema += features-unknown-key.json
qapi-schema += flat-union-array-branch.json
qapi-schema += flat-union-bad-base.json
qapi-schema += flat-union-bad-discriminator.json
qapi-schema += flat-union-base-any.json
qapi-schema += flat-union-base-union.json
qapi-schema += flat-union-clash-member.json
qapi-schema += flat-union-discriminator-bad-name.json
qapi-schema += flat-union-empty.json
qapi-schema += flat-union-inline.json
qapi-schema += flat-union-inline-invalid-dict.json
qapi-schema += flat-union-int-branch.json
qapi-schema += flat-union-invalid-branch-key.json
qapi-schema += flat-union-invalid-discriminator.json
qapi-schema += flat-union-invalid-if-discriminator.json
qapi-schema += flat-union-no-base.json
qapi-schema += flat-union-optional-discriminator.json
qapi-schema += flat-union-string-discriminator.json
qapi-schema += funny-char.json
qapi-schema += funny-word.json
qapi-schema += ident-with-escape.json
qapi-schema += include-before-err.json
qapi-schema += include-cycle.json
qapi-schema += include-extra-junk.json
qapi-schema += include-nested-err.json
qapi-schema += include-no-file.json
qapi-schema += include-non-file.json
qapi-schema += include-repetition.json
qapi-schema += include-self-cycle.json
qapi-schema += include-simple.json
qapi-schema += indented-expr.json
qapi-schema += leading-comma-list.json
qapi-schema += leading-comma-object.json
qapi-schema += missing-colon.json
qapi-schema += missing-comma-list.json
qapi-schema += missing-comma-object.json
qapi-schema += missing-type.json
qapi-schema += nested-struct-data.json
qapi-schema += nested-struct-data-invalid-dict.json
qapi-schema += non-objects.json
qapi-schema += oob-test.json
qapi-schema += allow-preconfig-test.json
qapi-schema += pragma-doc-required-crap.json
qapi-schema += pragma-extra-junk.json
qapi-schema += pragma-name-case-whitelist-crap.json
qapi-schema += pragma-non-dict.json
qapi-schema += pragma-unknown.json
qapi-schema += pragma-returns-whitelist-crap.json
qapi-schema += qapi-schema-test.json
qapi-schema += quoted-structural-chars.json
qapi-schema += redefined-builtin.json
qapi-schema += redefined-command.json
qapi-schema += redefined-event.json
qapi-schema += redefined-type.json
qapi-schema += reserved-command-q.json
qapi-schema += reserved-enum-q.json
qapi-schema += reserved-member-has.json
qapi-schema += reserved-member-q.json
qapi-schema += reserved-member-u.json
qapi-schema += reserved-member-underscore.json
qapi-schema += reserved-type-kind.json
qapi-schema += reserved-type-list.json
qapi-schema += returns-alternate.json
qapi-schema += returns-array-bad.json
qapi-schema += returns-dict.json
qapi-schema += returns-unknown.json
qapi-schema += returns-whitelist.json
qapi-schema += string-code-point-31.json
qapi-schema += string-code-point-127.json
qapi-schema += struct-base-clash-deep.json
qapi-schema += struct-base-clash.json
qapi-schema += struct-data-invalid.json
qapi-schema += struct-member-if-invalid.json
qapi-schema += struct-member-invalid-dict.json
qapi-schema += struct-member-invalid.json
qapi-schema += trailing-comma-list.json
qapi-schema += trailing-comma-object.json
qapi-schema += type-bypass-bad-gen.json
qapi-schema += unclosed-list.json
qapi-schema += unclosed-object.json
qapi-schema += unclosed-string.json
qapi-schema += union-base-empty.json
qapi-schema += union-base-no-discriminator.json
qapi-schema += union-branch-case.json
qapi-schema += union-branch-if-invalid.json
qapi-schema += union-branch-invalid-dict.json
qapi-schema += union-clash-branches.json
qapi-schema += union-empty.json
qapi-schema += union-invalid-base.json
qapi-schema += union-optional-branch.json
qapi-schema += union-unknown.json
qapi-schema += unknown-escape.json
qapi-schema += unknown-expr-key.json


check-qapi-schema-y := $(addprefix tests/qapi-schema/, $(qapi-schema))

generated-files-y += tests/test-qapi-types.h
generated-files-y += tests/include/test-qapi-types-sub-module.h
generated-files-y += tests/test-qapi-types-sub-sub-module.h
generated-files-y += tests/test-qapi-visit.h
generated-files-y += tests/include/test-qapi-visit-sub-module.h
generated-files-y += tests/test-qapi-visit-sub-sub-module.h
generated-files-y += tests/test-qapi-commands.h
generated-files-y += tests/test-qapi-init-commands.h
generated-files-y += tests/include/test-qapi-commands-sub-module.h
generated-files-y += tests/test-qapi-commands-sub-sub-module.h
generated-files-y += tests/test-qapi-emit-events.h
generated-files-y += tests/test-qapi-events.h
generated-files-y += tests/include/test-qapi-events-sub-module.h
generated-files-y += tests/test-qapi-events-sub-sub-module.h
generated-files-y += tests/test-qapi-introspect.h

QEMU_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest


# Deps that are common to various different sets of tests below
test-util-obj-y = libqemuutil.a
test-qom-obj-y = $(qom-obj-y) $(test-util-obj-y)
test-qapi-obj-y = tests/test-qapi-types.o \
	tests/include/test-qapi-types-sub-module.o \
	tests/test-qapi-types-sub-sub-module.o \
	tests/test-qapi-visit.o \
	tests/include/test-qapi-visit-sub-module.o \
	tests/test-qapi-visit-sub-sub-module.o \
	tests/test-qapi-introspect.o \
	$(test-qom-obj-y)
benchmark-crypto-obj-$(CONFIG_BLOCK) = $(authz-obj-y) $(crypto-obj-y) $(test-qom-obj-y)
test-crypto-obj-$(CONFIG_BLOCK) = $(authz-obj-y) $(crypto-obj-y) $(test-qom-obj-y)
test-io-obj-$(CONFIG_BLOCK) = $(io-obj-y) $(test-crypto-obj-y)
test-authz-obj-$(CONFIG_BLOCK) = $(test-qom-obj-y) $(authz-obj-y)
test-block-obj-$(CONFIG_BLOCK) = $(block-obj-y) $(test-io-obj-y) tests/iothread.o

tests/check-qnum$(EXESUF): tests/check-qnum.o $(test-util-obj-y)
tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
tests/check-block-qdict$(EXESUF): tests/check-block-qdict.o $(test-util-obj-y)
tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)
tests/check-qnull$(EXESUF): tests/check-qnull.o $(test-util-obj-y)
tests/check-qobject$(EXESUF): tests/check-qobject.o $(test-util-obj-y)
tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y)
tests/check-qlit$(EXESUF): tests/check-qlit.o $(test-util-obj-y)
tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y)
tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y)

tests/test-char$(EXESUF): tests/test-char.o $(test-util-obj-y) $(test-io-obj-y) $(chardev-obj-y) tests/socket-helpers.o
tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y)
tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y)
tests/test-aio-multithread$(EXESUF): tests/test-aio-multithread.o $(test-block-obj-y)
tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y)
tests/test-bdrv-drain$(EXESUF): tests/test-bdrv-drain.o $(test-block-obj-y) $(test-util-obj-y)
tests/test-bdrv-graph-mod$(EXESUF): tests/test-bdrv-graph-mod.o $(test-block-obj-y) $(test-util-obj-y)
tests/test-blockjob$(EXESUF): tests/test-blockjob.o $(test-block-obj-y) $(test-util-obj-y)
tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) $(test-util-obj-y)
tests/test-block-backend$(EXESUF): tests/test-block-backend.o $(test-block-obj-y) $(test-util-obj-y)
tests/test-block-iothread$(EXESUF): tests/test-block-iothread.o $(test-block-obj-y) $(test-util-obj-y)
tests/test-image-locking$(EXESUF): tests/test-image-locking.o $(test-block-obj-y) $(test-util-obj-y)
tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y)
tests/test-iov$(EXESUF): tests/test-iov.o $(test-util-obj-y)
tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o $(test-util-obj-y) $(test-crypto-obj-y)
tests/test-bitmap$(EXESUF): tests/test-bitmap.o $(test-util-obj-y)
tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o
tests/test-xbzrle$(EXESUF): tests/test-xbzrle.o migration/xbzrle.o migration/page_cache.o $(test-util-obj-y)
tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o $(test-util-obj-y)
tests/test-int128$(EXESUF): tests/test-int128.o
tests/rcutorture$(EXESUF): tests/rcutorture.o $(test-util-obj-y)
tests/test-rcu-list$(EXESUF): tests/test-rcu-list.o $(test-util-obj-y)
tests/test-rcu-simpleq$(EXESUF): tests/test-rcu-simpleq.o $(test-util-obj-y)
tests/test-rcu-tailq$(EXESUF): tests/test-rcu-tailq.o $(test-util-obj-y)
tests/test-rcu-slist$(EXESUF): tests/test-rcu-slist.o $(test-util-obj-y)
tests/test-qdist$(EXESUF): tests/test-qdist.o $(test-util-obj-y)
tests/test-qht$(EXESUF): tests/test-qht.o $(test-util-obj-y)
tests/test-qht-par$(EXESUF): tests/test-qht-par.o tests/qht-bench$(EXESUF) $(test-util-obj-y)
tests/qht-bench$(EXESUF): tests/qht-bench.o $(test-util-obj-y)
tests/test-bufferiszero$(EXESUF): tests/test-bufferiszero.o $(test-util-obj-y)
tests/atomic_add-bench$(EXESUF): tests/atomic_add-bench.o $(test-util-obj-y)
tests/atomic64-bench$(EXESUF): tests/atomic64-bench.o $(test-util-obj-y)

tests/fp/%:
	$(MAKE) -C $(dir $@) $(notdir $@)

tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
	hw/core/qdev.o hw/core/qdev-properties.o hw/core/hotplug.o\
	hw/core/bus.o \
	hw/core/resettable.o \
	hw/core/irq.o \
	hw/core/fw-path-provider.o \
	hw/core/reset.o \
	hw/core/vmstate-if.o \
	hw/core/clock.o hw/core/qdev-clock.o \
	$(test-qapi-obj-y)
tests/test-vmstate$(EXESUF): tests/test-vmstate.o \
	migration/vmstate.o migration/vmstate-types.o migration/qemu-file.o \
        migration/qemu-file-channel.o migration/qjson.o \
	$(test-io-obj-y)
tests/test-timed-average$(EXESUF): tests/test-timed-average.o $(test-util-obj-y)
tests/test-base64$(EXESUF): tests/test-base64.o $(test-util-obj-y)
tests/ptimer-test$(EXESUF): tests/ptimer-test.o tests/ptimer-test-stubs.o hw/core/ptimer.o
tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y)
tests/test-keyval$(EXESUF): tests/test-keyval.o $(test-util-obj-y) $(test-qapi-obj-y)
tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y)
tests/test-uuid$(EXESUF): tests/test-uuid.o $(test-util-obj-y)
tests/test-qapi-util$(EXESUF): tests/test-qapi-util.o $(test-util-obj-y)

tests/test-logging$(EXESUF): tests/test-logging.o $(test-util-obj-y)

tests/test-replication$(EXESUF): tests/test-replication.o $(test-util-obj-y) \
	$(test-block-obj-y)

tests/test-qapi-types.c tests/test-qapi-types.h \
tests/include/test-qapi-types-sub-module.c \
tests/include/test-qapi-types-sub-module.h \
tests/test-qapi-types-sub-sub-module.c \
tests/test-qapi-types-sub-sub-module.h \
tests/test-qapi-visit.c tests/test-qapi-visit.h \
tests/include/test-qapi-visit-sub-module.c \
tests/include/test-qapi-visit-sub-module.h \
tests/test-qapi-visit-sub-sub-module.c \
tests/test-qapi-visit-sub-sub-module.h \
tests/test-qapi-commands.h tests/test-qapi-commands.c \
tests/include/test-qapi-commands-sub-module.h \
tests/include/test-qapi-commands-sub-module.c \
tests/test-qapi-commands-sub-sub-module.h \
tests/test-qapi-commands-sub-sub-module.c \
tests/test-qapi-emit-events.c tests/test-qapi-emit-events.h \
tests/test-qapi-events.c tests/test-qapi-events.h \
tests/test-qapi-init-commands.c \
tests/test-qapi-init-commands.h \
tests/include/test-qapi-events-sub-module.c \
tests/include/test-qapi-events-sub-module.h \
tests/test-qapi-events-sub-sub-module.c \
tests/test-qapi-events-sub-sub-module.h \
tests/test-qapi-introspect.c tests/test-qapi-introspect.h: \
tests/test-qapi-gen-timestamp ;
tests/test-qapi-gen-timestamp: \
		$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json \
		$(SRC_PATH)/tests/qapi-schema/include/sub-module.json \
		$(SRC_PATH)/tests/qapi-schema/sub-sub-module.json \
		$(qapi-py)
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-gen.py \
		-o tests -p "test-" $<, \
		"GEN","$(@:%-timestamp=%)")
	@rm -f tests/test-qapi-doc.texi
	@>$@

tests/qapi-schema/doc-good.test.texi: $(SRC_PATH)/tests/qapi-schema/doc-good.json $(qapi-py)
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-gen.py \
		-o tests/qapi-schema -p "doc-good-" $<, \
		"GEN","$@")
	@mv tests/qapi-schema/doc-good-qapi-doc.texi $@
	@rm -f tests/qapi-schema/doc-good-qapi-*.[ch] tests/qapi-schema/doc-good-qmp-*.[ch]

tests/qtest/dbus-vmstate1.h tests/qtest/dbus-vmstate1.c: tests/qtest/dbus-vmstate1-gen-timestamp ;
tests/qtest/dbus-vmstate1-gen-timestamp: $(SRC_PATH)/tests/qtest/dbus-vmstate1.xml
	$(call quiet-command,$(GDBUS_CODEGEN) $< \
		--interface-prefix org.qemu --generate-c-code tests/qtest/dbus-vmstate1, \
		"GEN","$(@:%-timestamp=%)")
	@>$@

tests/qtest/dbus-vmstate-test.o-cflags := -DSRCDIR="$(SRC_PATH)"
tests/qtest/dbus-vmstate1.o-cflags := $(GIO_CFLAGS)
tests/qtest/dbus-vmstate1.o-libs := $(GIO_LIBS)

tests/qtest/dbus-vmstate-test.o: tests/qtest/dbus-vmstate1.h

tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(test-qapi-obj-y)
tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y)
tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y) tests/test-qapi-emit-events.o tests/test-qapi-events.o
tests/test-qobject-output-visitor$(EXESUF): tests/test-qobject-output-visitor.o $(test-qapi-obj-y)
tests/test-clone-visitor$(EXESUF): tests/test-clone-visitor.o $(test-qapi-obj-y)
tests/test-qobject-input-visitor$(EXESUF): tests/test-qobject-input-visitor.o $(test-qapi-obj-y)
tests/test-qmp-cmds$(EXESUF): tests/test-qmp-cmds.o tests/test-qapi-commands.o tests/test-qapi-init-commands.o $(test-qapi-obj-y)
tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $(test-qapi-obj-y)
tests/test-opts-visitor$(EXESUF): tests/test-opts-visitor.o $(test-qapi-obj-y)

tests/test-shift128$(EXESUF): tests/test-shift128.o $(test-util-obj-y)
tests/test-mul64$(EXESUF): tests/test-mul64.o $(test-util-obj-y)
tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y)
tests/test-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-util-obj-y)
tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y)
tests/benchmark-crypto-hash$(EXESUF): tests/benchmark-crypto-hash.o $(test-crypto-obj-y)
tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-obj-y)
tests/benchmark-crypto-hmac$(EXESUF): tests/benchmark-crypto-hmac.o $(test-crypto-obj-y)
tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y)
tests/benchmark-crypto-cipher$(EXESUF): tests/benchmark-crypto-cipher.o $(test-crypto-obj-y)
tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypto-obj-y)
tests/test-crypto-xts$(EXESUF): tests/test-crypto-xts.o $(test-crypto-obj-y)

ifeq ($(CONFIG_TEST_SECRET_KEYRING),y)
tests/test-crypto-secret.o-libs := -lkeyutils
endif

tests/crypto-tls-x509-helpers.o-cflags := $(TASN1_CFLAGS)
tests/crypto-tls-x509-helpers.o-libs := $(TASN1_LIBS)
tests/pkix_asn1_tab.o-cflags := $(TASN1_CFLAGS)

tests/test-crypto-tlscredsx509.o-cflags := $(TASN1_CFLAGS)
tests/test-crypto-tlscredsx509$(EXESUF): tests/test-crypto-tlscredsx509.o \
	tests/crypto-tls-x509-helpers.o tests/pkix_asn1_tab.o $(test-crypto-obj-y)

tests/test-crypto-tlssession.o-cflags := $(TASN1_CFLAGS)
tests/test-crypto-tlssession$(EXESUF): tests/test-crypto-tlssession.o \
	tests/crypto-tls-x509-helpers.o tests/pkix_asn1_tab.o \
	tests/crypto-tls-psk-helpers.o \
        $(test-crypto-obj-y)
tests/test-util-filemonitor$(EXESUF): tests/test-util-filemonitor.o \
	$(test-util-obj-y)
tests/test-util-sockets$(EXESUF): tests/test-util-sockets.o \
	tests/socket-helpers.o $(test-util-obj-y)
tests/test-authz-simple$(EXESUF): tests/test-authz-simple.o $(test-authz-obj-y)
tests/test-authz-list$(EXESUF): tests/test-authz-list.o $(test-authz-obj-y)
tests/test-authz-listfile$(EXESUF): tests/test-authz-listfile.o $(test-authz-obj-y)
tests/test-authz-pam$(EXESUF): tests/test-authz-pam.o $(test-authz-obj-y)
tests/test-io-task$(EXESUF): tests/test-io-task.o $(test-io-obj-y)
tests/test-io-channel-socket$(EXESUF): tests/test-io-channel-socket.o \
        tests/io-channel-helpers.o tests/socket-helpers.o $(test-io-obj-y)
tests/test-io-channel-file$(EXESUF): tests/test-io-channel-file.o \
        tests/io-channel-helpers.o $(test-io-obj-y)
tests/test-io-channel-tls$(EXESUF): tests/test-io-channel-tls.o \
	tests/crypto-tls-x509-helpers.o tests/pkix_asn1_tab.o \
	tests/io-channel-helpers.o $(test-io-obj-y)
tests/test-io-channel-command$(EXESUF): tests/test-io-channel-command.o \
        tests/io-channel-helpers.o $(test-io-obj-y)
tests/test-io-channel-buffer$(EXESUF): tests/test-io-channel-buffer.o \
        tests/io-channel-helpers.o $(test-io-obj-y)
tests/test-crypto-pbkdf$(EXESUF): tests/test-crypto-pbkdf.o $(test-crypto-obj-y)
tests/test-crypto-ivgen$(EXESUF): tests/test-crypto-ivgen.o $(test-crypto-obj-y)
tests/test-crypto-afsplit$(EXESUF): tests/test-crypto-afsplit.o $(test-crypto-obj-y)
tests/test-crypto-block$(EXESUF): tests/test-crypto-block.o $(test-crypto-obj-y)

tests/migration/stress$(EXESUF): tests/migration/stress.o
	$(call quiet-command, $(LINKPROG) -static -O3 $(PTHREAD_LIB) -o $@ $< ,"LINK","$(TARGET_DIR)$@")

INITRD_WORK_DIR=tests/migration/initrd

tests/migration/initrd-stress.img: tests/migration/stress$(EXESUF)
	mkdir -p $(INITRD_WORK_DIR)
	cp $< $(INITRD_WORK_DIR)/init
	(cd $(INITRD_WORK_DIR) && (find | cpio --quiet -o -H newc | gzip -9)) > $@
	rm $(INITRD_WORK_DIR)/init
	rmdir $(INITRD_WORK_DIR)

include $(SRC_PATH)/tests/qtest/Makefile.include

tests/test-qga$(EXESUF): qemu-ga$(EXESUF)
tests/test-qga$(EXESUF): tests/test-qga.o $(qtest-obj-y)
tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o $(test-util-obj-y) libvhost-user.a
tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o

SPEED = quick

# gtester tests, possibly with verbose output
# do_test_tap runs all tests, even if some of them fail, while do_test_human
# stops at the first failure unless -k is given on the command line

define do_test_human_k
        $(quiet-@)rc=0; $(foreach COMMAND, $1, \
          $(call quiet-command-run, \
            export MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))} $2; \
              $(COMMAND) -m=$(SPEED) -k --tap < /dev/null \
              | ./scripts/tap-driver.pl --test-name="$(notdir $(COMMAND))" $(if $(V),, --show-failures-only) \
              || rc=$$?;, "TEST", "$@: $(COMMAND)")) exit $$rc
endef
define do_test_human_no_k
        $(foreach COMMAND, $1, \
          $(call quiet-command, \
            MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))} $2 \
              $(COMMAND) -m=$(SPEED) -k --tap < /dev/null \
              | ./scripts/tap-driver.pl --test-name="$(notdir $(COMMAND))" $(if $(V),, --show-failures-only), \
              "TEST", "$@: $(COMMAND)")
)
endef
do_test_human = \
        $(if $(findstring k, $(MAKEFLAGS)), $(do_test_human_k), $(do_test_human_no_k))

define do_test_tap
	$(call quiet-command, \
          { export MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))} $2; \
            $(foreach COMMAND, $1, \
	      $(COMMAND) -m=$(SPEED) -k --tap < /dev/null \
	      | sed "s/^\(not \)\?ok [0-9]* /&$(notdir $(COMMAND)) /" || true; ) } \
	      | ./scripts/tap-merge.pl | tee "$@" \
	      | ./scripts/tap-driver.pl $(if $(V),, --show-failures-only), \
	  "TAP","$@")
endef

.PHONY: $(patsubst %, check-qtest-%, $(QTEST_TARGETS))
$(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-qtest-%: %-softmmu/all $(check-qtest-y)
	$(call do_test_human,$(check-qtest-$*-y:%=tests/qtest/%$(EXESUF)) $(check-qtest-generic-y:%=tests/qtest/%$(EXESUF)), \
	  QTEST_QEMU_BINARY=$*-softmmu/qemu-system-$* \
	  QTEST_QEMU_IMG=qemu-img$(EXESUF))

build-qtest: $(patsubst %, %-softmmu/all, $(QTEST_TARGETS)) $(check-qtest-y)

build-unit: $(check-unit-y)

check-unit: $(check-unit-y)
	$(call do_test_human, $^)

check-speed: $(check-speed-y)
	$(call do_test_human, $^)

# gtester tests with TAP output

$(patsubst %, check-report-qtest-%.tap, $(QTEST_TARGETS)): check-report-qtest-%.tap: %-softmmu/all $(check-qtest-y)
	$(call do_test_tap, $(check-qtest-$*-y:%=tests/qtest/%$(EXESUF)) $(check-qtest-generic-y:%=tests/qtest/%$(EXESUF)), \
	  QTEST_QEMU_BINARY=$*-softmmu/qemu-system-$* \
	  QTEST_QEMU_IMG=qemu-img$(EXESUF))

check-report-unit.tap: $(check-unit-y)
	$(call do_test_tap,$^)

# Reports and overall runs

check-report.tap: $(patsubst %,check-report-qtest-%.tap, $(QTEST_TARGETS)) check-report-unit.tap
	$(call quiet-command, cat $^ | scripts/tap-merge.pl >$@,"GEN","$@")

# FPU Emulation tests (aka softfloat)
#
# As we still have some places that need fixing the rules are a little
# more complex than they need to be and have to override some of the
# generic Makefile expansions. Once we are cleanly passing all
# the tests we can simplify the make syntax.

FP_TEST_BIN=$(BUILD_DIR)/tests/fp/fp-test

# the build dir is created by configure
$(FP_TEST_BIN): config-host.h $(test-util-obj-y)
	$(call quiet-command, \
	 	$(MAKE) $(SUBDIR_MAKEFLAGS) -C $(dir $@) V="$(V)" $(notdir $@), \
	         "BUILD", "$(notdir $@)")

# The full test suite can take a bit of time, default to a quick run
# "-l 2 -r all" can take more than a day for some operations and is best
# run manually
FP_TL=-l 1 -r all

# $1 = tests, $2 = description, $3 = test flags
test-softfloat = $(call quiet-command, \
			cd $(BUILD_DIR)/tests/fp && \
			./fp-test -s $(if $3,$3,$(FP_TL)) $1 > $2.out 2>&1 || \
			(cat $2.out && exit 1;), \
			"FLOAT TEST", $2)

# Conversion Routines: Float to Float
# FIXME: f32_to_f128 (broken), f64_to_f128 (broken)
# FIXME: f128_to_f32(broken), f128_to_f64 (broken)
# FIXME: f128_to_extF80 (broken)
check-softfloat-conv-f2f: $(FP_TEST_BIN)
	$(call test-softfloat, \
		f16_to_f32 f16_to_f64 \
		f16_to_extF80 f16_to_f128 \
		f32_to_f16 f32_to_f64 \
		f32_to_extF80 \
		f64_to_f16 f64_to_f32 \
		extF80_to_f16 extF80_to_f32 \
		extF80_to_f64 extF80_to_f128 \
		f128_to_f16, \
		float-to-float)

# Conversion Routines: Int and Uint to Float
# FIXME: i32_to_extF80 (broken), i64_to_extF80 (broken)
#        ui32_to_f128 (not implemented)
check-softfloat-conv-to-float: $(FP_TEST_BIN)
	$(call test-softfloat, \
		i32_to_f16 i64_to_f16 \
		i32_to_f32 i64_to_f32 \
		i32_to_f64 i64_to_f64 \
		i32_to_f128 i64_to_f128, int-to-float)
	$(call test-softfloat, \
		ui32_to_f16 ui64_to_f16 \
		ui32_to_f32 ui64_to_f32 \
		ui32_to_f64 ui64_to_f64 \
		ui32_to_extF80 ui64_to_extF80 \
		ui64_to_f128, uint-to-float)

# Conversion Routines: Float to integers
# FIXME: extF80_roundToInt (broken)
check-softfloat-conv-to-int: $(FP_TEST_BIN)
	$(call test-softfloat, \
		f16_to_i32 f16_to_i32_r_minMag \
		f32_to_i32 f32_to_i32_r_minMag \
		f64_to_i32 f64_to_i32_r_minMag \
		extF80_to_i32 extF80_to_i32_r_minMag \
		f128_to_i32 f128_to_i32_r_minMag \
		f16_to_i64 f16_to_i64_r_minMag \
		f32_to_i64 f32_to_i64_r_minMag \
		f64_to_i64 f64_to_i64_r_minMag \
		extF80_to_i64 extF80_to_i64_r_minMag \
		f128_to_i64 f128_to_i64_r_minMag, \
		float-to-int)
	$(call test-softfloat, \
		f16_to_ui32 f16_to_ui32_r_minMag \
		f32_to_ui32 f32_to_ui32_r_minMag \
		f64_to_ui32 f64_to_ui32_r_minMag \
		extF80_to_ui32 extF80_to_ui32_r_minMag \
		f128_to_ui32 f128_to_ui32_r_minMag \
		f16_to_ui64 f16_to_ui64_r_minMag \
		f32_to_ui64 f32_to_ui64_r_minMag \
		f64_to_ui64 f64_to_ui64_r_minMag \
		extF80_to_ui64 extF80_to_ui64_r_minMag \
		f128_to_ui64 f128_to_ui64_r_minMag, \
		float-to-uint)
	$(call test-softfloat, \
		f16_roundToInt f32_roundToInt \
		f64_roundToInt f128_roundToInt, \
		round-to-integer)

.PHONY: check-softfloat-conv
check-softfloat-conv: check-softfloat-conv-f2f
check-softfloat-conv: check-softfloat-conv-to-float
check-softfloat-conv: check-softfloat-conv-to-int

# Generic rule for all float operations
#
# Some patterns are overridden due to broken or missing tests.
# Hopefully these can be removed over time.

check-softfloat-%: $(FP_TEST_BIN)
	$(call test-softfloat, f16_$* f32_$* f64_$* extF80_$* f128_$*, $*)

# Float Compare routines
SF_COMPARE_OPS=eq eq_signaling le le_quiet lt_quiet
SF_COMPARE_RULES=$(patsubst %,check-softfloat-%, $(SF_COMPARE_OPS))

# FIXME: extF80_lt_quiet (broken)
check-softfloat-lt_quiet: $(FP_TEST_BIN)
	$(call test-softfloat, 				\
		f16_lt_quiet f32_lt_quiet f64_lt_quiet  \
		f128_lt_quiet, 				\
		lt_quiet)

.PHONY: check-softfloat-compare
check-softfloat-compare: $(SF_COMPARE_RULES)

# Math Operations

# FIXME: extF80_mulAdd (missing)
check-softfloat-mulAdd: $(FP_TEST_BIN)
	$(call test-softfloat, \
		f16_mulAdd f32_mulAdd f64_mulAdd f128_mulAdd, \
		mulAdd,-l 1)

# FIXME: extF80_rem (broken)
check-softfloat-rem: $(FP_TEST_BIN)
	$(call test-softfloat, \
		f16_rem f32_rem f64_rem f128_rem, \
		rem)

SF_MATH_OPS=add sub mul mulAdd div rem sqrt
SF_MATH_RULES=$(patsubst %,check-softfloat-%, $(SF_MATH_OPS))

.PHONY: check-softfloat-ops
check-softfloat-ops: $(SF_MATH_RULES)

# Finally a generic rule to test all of softfoat. If TCG isnt't
# enabled we define a null operation which skips the tests.

.PHONY: check-softfloat
ifeq ($(CONFIG_TCG),y)
build-softfloat: $(FP_TEST_BIN)
check-softfloat: build-softfloat check-softfloat-conv check-softfloat-compare check-softfloat-ops
else
build-softfloat check-softfloat:
	$(call quiet-command, /bin/true, "FLOAT TEST", \
		"SKIPPED for non-TCG builds")
endif

# Plugins
ifeq ($(CONFIG_PLUGIN),y)
.PHONY: plugins
plugins:
	$(call quiet-command,\
		$(MAKE) $(SUBDIR_MAKEFLAGS) -C tests/plugin V="$(V)", \
		"BUILD", "plugins")
endif

# Per guest TCG tests

BUILD_TCG_TARGET_RULES=$(patsubst %,build-tcg-tests-%, $(TARGET_DIRS))
CLEAN_TCG_TARGET_RULES=$(patsubst %,clean-tcg-tests-%, $(TARGET_DIRS))
RUN_TCG_TARGET_RULES=$(patsubst %,run-tcg-tests-%, $(TARGET_DIRS))

# Probe for the Docker Builds needed for each build
$(foreach PROBE_TARGET,$(TARGET_DIRS), 				\
	$(eval -include $(SRC_PATH)/tests/tcg/Makefile.prereqs))

build-tcg-tests-%: $(if $(CONFIG_PLUGIN),plugins)
	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) \
		-f $(SRC_PATH)/tests/tcg/Makefile.qemu \
		SRC_PATH=$(SRC_PATH) \
	       	V="$(V)" TARGET="$*" guest-tests, \
		"BUILD", "TCG tests for $*")

run-tcg-tests-%: build-tcg-tests-% %/all
	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) \
		-f $(SRC_PATH)/tests/tcg/Makefile.qemu \
		SRC_PATH=$(SRC_PATH) SPEED="$(SPEED)" \
		V="$(V)" TARGET="$*" run-guest-tests, \
		"RUN", "TCG tests for $*")

clean-tcg-tests-%:
	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) \
		-f $(SRC_PATH)/tests/tcg/Makefile.qemu \
		SRC_PATH=$(SRC_PATH) TARGET="$*" clean-guest-tests, \
		"CLEAN", "TCG tests for $*")

.PHONY: build-tcg
build-tcg: $(BUILD_TCG_TARGET_RULES)

.PHONY: check-tcg
check-tcg: $(RUN_TCG_TARGET_RULES)

.PHONY: clean-tcg
clean-tcg: $(CLEAN_TCG_TARGET_RULES)

# Other tests

QEMU_IOTESTS_HELPERS-$(call land,$(CONFIG_SOFTMMU),$(CONFIG_LINUX)) = tests/qemu-iotests/socket_scm_helper$(EXESUF)

.PHONY: check-tests/check-block.sh
check-tests/check-block.sh: tests/check-block.sh qemu-img$(EXESUF) \
		qemu-io$(EXESUF) qemu-nbd$(EXESUF) $(QEMU_IOTESTS_HELPERS-y) \
		$(patsubst %,%/all,$(filter %-softmmu,$(TARGET_DIRS)))
	@$<

.PHONY: check-tests/qapi-schema/frontend
check-tests/qapi-schema/frontend: $(addprefix $(SRC_PATH)/, $(check-qapi-schema-y))
	$(call quiet-command, PYTHONPATH=$(SRC_PATH)/scripts \
	  PYTHONIOENCODING=utf-8 $(PYTHON) $(SRC_PATH)/tests/qapi-schema/test-qapi.py $^, \
	  TEST, check-qapi-schema)

.PHONY: check-tests/qapi-schema/doc-good.texi
check-tests/qapi-schema/doc-good.texi: tests/qapi-schema/doc-good.test.texi
	@diff -u $(SRC_PATH)/tests/qapi-schema/doc-good.texi $<

.PHONY: check-decodetree
check-decodetree:
	$(call quiet-command, \
	  cd $(SRC_PATH)/tests/decode && \
          ./check.sh "$(PYTHON)" "$(SRC_PATH)/scripts/decodetree.py", \
          TEST, decodetree.py)

# Python venv for running tests

.PHONY: check-venv check-acceptance

TESTS_VENV_DIR=$(BUILD_DIR)/tests/venv
TESTS_VENV_REQ=$(SRC_PATH)/tests/requirements.txt
TESTS_RESULTS_DIR=$(BUILD_DIR)/tests/results
# Controls the output generated by Avocado when running tests.
# Any number of command separated loggers are accepted.  For more
# information please refer to "avocado --help".
AVOCADO_SHOW=app
AVOCADO_TAGS=$(patsubst %-softmmu,-t arch:%, $(filter %-softmmu,$(TARGET_DIRS)))

$(TESTS_VENV_DIR): $(TESTS_VENV_REQ)
	$(call quiet-command, \
            $(PYTHON) -m venv --system-site-packages $@, \
            VENV, $@)
	$(call quiet-command, \
            $(TESTS_VENV_DIR)/bin/python -m pip -q install -r $(TESTS_VENV_REQ), \
            PIP, $(TESTS_VENV_REQ))
	$(call quiet-command, touch $@)

$(TESTS_RESULTS_DIR):
	$(call quiet-command, mkdir -p $@, \
            MKDIR, $@)

check-venv: $(TESTS_VENV_DIR)

FEDORA_31_ARCHES_CANDIDATES=$(patsubst ppc64,ppc64le,$(TARGETS))
FEDORA_31_ARCHES := x86_64 aarch64 ppc64le s390x
FEDORA_31_DOWNLOAD=$(filter $(FEDORA_31_ARCHES),$(FEDORA_31_ARCHES_CANDIDATES))

# download one specific Fedora 31 image
get-vm-image-fedora-31-%: check-venv
	$(call quiet-command, \
             $(TESTS_VENV_DIR)/bin/python -m avocado vmimage get \
             --distro=fedora --distro-version=31 --arch=$*, \
	"AVOCADO", "Downloading acceptance tests VM image for $*")

# download all vm images, according to defined targets
get-vm-images: check-venv $(patsubst %,get-vm-image-fedora-31-%, $(FEDORA_31_DOWNLOAD))

check-acceptance: check-venv $(TESTS_RESULTS_DIR) get-vm-images
	$(call quiet-command, \
            $(TESTS_VENV_DIR)/bin/python -m avocado \
            --show=$(AVOCADO_SHOW) run --job-results-dir=$(TESTS_RESULTS_DIR) \
            --filter-by-tags-include-empty --filter-by-tags-include-empty-key \
            $(AVOCADO_TAGS) \
            $(if $(GITLAB_CI),,--failfast=on) tests/acceptance, \
            "AVOCADO", "tests/acceptance")

# Consolidated targets

.PHONY: check-block check-qapi-schema check-qtest check-unit check check-clean get-vm-images
check-qapi-schema: check-tests/qapi-schema/frontend check-tests/qapi-schema/doc-good.texi
check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS))
ifeq ($(CONFIG_TOOLS),y)
check-block: $(patsubst %,check-%, $(check-block-y))
endif
check-build: build-unit build-softfloat build-qtest

check-clean:
	rm -rf $(check-unit-y) tests/*.o tests/*/*.o $(QEMU_IOTESTS_HELPERS-y)
	rm -rf $(sort $(foreach target,$(SYSEMU_TARGET_LIST), $(check-qtest-$(target)-y:%=tests/qtest/%$(EXESUF))) $(check-qtest-generic-y:%=tests/qtest/%$(EXESUF)))
	rm -f tests/test-qapi-gen-timestamp
	rm -f tests/qtest/dbus-vmstate1-gen-timestamp
	rm -rf $(TESTS_VENV_DIR) $(TESTS_RESULTS_DIR)

check: check-block check-qapi-schema check-unit check-softfloat check-qtest check-decodetree

clean: check-clean

# Build the help program automatically

all: $(QEMU_IOTESTS_HELPERS-y)

-include $(wildcard tests/*.d)
-include $(wildcard tests/qtest/*.d)
-include $(wildcard tests/qtest/libqos/*.d)

endif
